package com.prs.crm.dao.crm.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.crm.DispatchPersonDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.crm.DispatchPerson;
public class DispatchPersonDaoImpl extends BaseDaoImpl<DispatchPerson>
		implements DispatchPersonDao {

	@SuppressWarnings("unchecked")
	@Override
	public int SerchPerson(final String name,final String site) {
		return (Integer) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = session
								.createCriteria(DispatchPerson.class);
						criteria.add(Restrictions.eq("peName", name));
						criteria.add(Restrictions.eq("peAdress", site));
						List<DispatchPerson> alist = criteria.list();
						if (alist == null) {
							return 0;
						} else {
							return alist.size();
						}

					}
				});
	}
	@SuppressWarnings("unchecked")
	@Override
	public DispatchPerson getDispatchPerson(final String name,final String site) {
		return (DispatchPerson) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = session
								.createCriteria(DispatchPerson.class);
						criteria.add(Restrictions.eq("peName", name));
						criteria.add(Restrictions.eq("peAdress", site));
						List<DispatchPerson>  resuls=criteria.list();
							if(resuls.size()==0){
								return null;
							}	else {				
							    return resuls.get(0);
							}
					

					}
				});
	}
	@Override
	public List<DispatchPerson> getSizePerson(final String name,final String site) {
		return (List<DispatchPerson>) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = session
								.createCriteria(DispatchPerson.class);
						criteria.add(Restrictions.eq("peName", name));
						criteria.add(Restrictions.eq("peAdress", site));
						List<DispatchPerson> alist = criteria.list();
						if (alist == null) {
							return alist;
						} else {
							return alist;
						}

					}
				});
	}

}
